'use strict';

/**
 * Created by twer on 15/3/26.
 */
angular.module('space')
    .controller('VisitorCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'SpaceVisitorService',
        'ProfilePhotoService',
        'PhotoHost',
        function ($scope, $rootScope, $routeParams, SpaceVisitorService, ProfilePhotoService, PhotoHost) {
            $rootScope.visitedUserId = $routeParams.userId;
            var ownerId = $routeParams.userId;

            SpaceVisitorService.query({ownerId: ownerId}, function (response) {
                $scope.visitors = response;

                angular.forEach($scope.visitors, function (visitor) {
                    ProfilePhotoService.query({userId: visitor.id}, function (response) {
                        visitor.photoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=50&h=50';
                    });
                });
            });
        }])

    .controller('SpaceCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'SpaceFriendMessageService',
        'ProfilePhotoService',
        'PhotoHost',
        'FriendRequestService',
        'FriendConfirmService',
        function ($scope, $rootScope, $routeParams, SpaceFriendMessageService, ProfilePhotoService, PhotoHost, FriendRequestService, FriendConfirmService) {
            $rootScope.visitedUserId = $routeParams.userId;
            var ownerId = $rootScope.currentUser.id;

            SpaceFriendMessageService.query({ownerId: ownerId}, function (response) {
                $scope.messages = [];
                angular.forEach(response.messages, function (v) {
                    var friendMessage = friendMessageTemplate[v.friendMessageType](v);
                    var photoOwnerId = v.ownerIsAcceptor ? v.requester.identity : v.acceptor.identity;
                    ProfilePhotoService.query({userId: photoOwnerId}, function (response) {
                        friendMessage.profilePhotoUri = PhotoHost + 'cdrmimg/' + response.photoId + '?w=48&h=48';
                    });
                    friendMessage.friendMessageType = v.friendMessageType;
                    friendMessage.messageId = v.messageId;
                    friendMessage.requesterId = v.requester.identity;
                    $scope.messages.push(friendMessage);
                });
            });

            var friendMessageTemplate = {
                "REQUEST": function (message) {
                    var requesterName = message.requester.name,
                        acceptorName = message.acceptor.name;
                    return friendMessage(
                        message.requestWords,
                        message.ownerIsAcceptor ? requesterName + '想加你为好友' : "你已向" + acceptorName + "发出邀请"
                    );
                },
                "REJECT": function (message) {
                    var requesterName = message.requester.name,
                        acceptorName = message.acceptor.name;
                    return friendMessage(
                        message.rejectWords,
                        message.ownerIsAcceptor ? requesterName + '拒绝了你的好友请求' : "你已拒绝" + acceptorName + "的请求"
                    );
                },
                "ADD": function (message) {
                    var requesterName = message.requester.name,
                        acceptorName = message.acceptor.name;
                    return friendMessage(
                        "",
                        message.ownerIsAcceptor ? requesterName + '已确认你为好友' : "你已确认" + acceptorName + "为好友"
                    );
                },
                "DELETE": function (message) {
                    var requesterName = message.requester.name,
                        acceptorName = message.acceptor.name;
                    return friendMessage(
                        "",
                        message.ownerIsAcceptor ? requesterName + '已将你删除' : "你已将" + acceptorName + "删除"
                    );
                }
            };

            function friendMessage(words, action) {
                return {
                    words: words,
                    action: action
                };
            }

            $scope.reject = function (message) {
                FriendRequestService.reject({
                    requesterId: message.requesterId,
                    acceptorId: ownerId,
                    messageId: message.messageId
                }, function () {
                    message.friendMessageType = 'REJECT';
                });
            };

            $scope.confirm = function (message) {
                FriendConfirmService.confirm({
                    requesterId: message.requesterId,
                    acceptorId: ownerId
                }, {
                    messageId: message.messageId
                }, function () {
                    message.friendMessageType = 'ADD';
                });
            };

            $scope.remove = function (message) {
                if (!confirm('是否要删除 "' + message.action + '" 这条消息？')) {
                    return;
                }

                FriendConfirmService.remove({
                    ownerId: ownerId,
                    messageId: message.messageId
                }, function () {
                    var newMessages = [];
                    angular.forEach($scope.messages, function (v) {
                        if (v.messageId != message.messageId) {
                            this.push(v);
                        }
                    }, newMessages);
                    $scope.messages = newMessages;
                });
            };

            $scope.showStatus = function (message, status) {
                return message.friendMessageType == status;
            };
        }])

    .controller('PasswordCtrl', [
        '$scope',
        '$rootScope',
        '$routeParams',
        'SpacePasswordService',
        function ($scope, $rootScope, $routeParams, SpacePasswordService) {
            $rootScope.visitedUserId = $routeParams.userId;
            $scope.userName = $rootScope.currentUser.name;

            $scope.change = function () {
                if (!validate()) {
                    return;
                }
                var pass = {
                    userId: $rootScope.currentUser.id,
                    oldPassword: $scope.oldPassword,
                    newPassword: $scope.newPassword
                }

                SpacePasswordService.change({ownerId: pass.userId}, pass,
                    function () {
                        alert('修改成功');
                        $scope.reset();
                    }, function () {
                        alert('密码不正确');
                        $scope.reset();
                    });
            };

            function validate() {
                if (!$scope.newPassword || !$scope.oldPassword || !$scope.confirmNewPassword) {
                    alert('所有项目都是必填项');
                    return false;
                }
                if ($scope.newPassword != $scope.confirmNewPassword) {
                    alert('新密码和确认密码不匹配，请重新输入！');
                    $scope.reset();
                    return false;
                }
                return true;
            }

            $scope.reset = function () {
                $scope.oldPassword = '';
                $scope.newPassword = '';
                $scope.confirmNewPassword = '';
            };
        }]);
